package com.outstanding.his.doctor.dao.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.outstanding.his.doctor.dao.IDoctorDao;
import com.outstanding.his.doctor.domain.Doctor;

public class HibernateDoctorDao extends HibernateDaoSupport implements
		IDoctorDao {

	@Override
	public Doctor submitDoctor(Doctor doctor) throws DataAccessException {
		this.getHibernateTemplate().save(doctor);
		return doctor;
	}

	@Override
	public Doctor getDoctor(Integer doctorId) throws DataAccessException {
		Doctor doctor = (Doctor) this.getSession().load(Doctor.class, doctorId);
		return doctor;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Doctor> getDoctorList() throws DataAccessException {
		Collection<Doctor> doctors = this.getSession().createQuery(
				"from Doctor").list();
		return doctors;
	}

	@Override
	public Doctor updateDoctor(Doctor doctor) throws DataAccessException {
		this.getHibernateTemplate().merge(doctor);
		return doctor;
	}

	@SuppressWarnings("unchecked")
	public Doctor loginDoctor(Doctor doctor) {
		List list = getHibernateTemplate().find("from Doctor d where d.doctorLoginName='"
				+ doctor.getDoctorLoginName() + "' and d.doctorPassword = '" + doctor.getDoctorPassword()
				+ "'");
		if (list != null && list.size() == 1) {
			return (Doctor) list.get(0);
		} else {
			return null;
		}
	}

}
